//防止ajax 访问url的时候缓存
function no_cache_url(a) {
	var b = new Date();
	if (a.indexOf("?") >= 0) {
		a = a + "&_s=" + b.getTime()
	} else {
		a = a + "?_s=" + b.getTime()
	}
	return a
}
// JavaScript Document
var bottom_cart = bottom_cart || {};
bottom_cart.active_group_id = 0;
bottom_cart.opened = false;
bottom_cart.load = function(b) {
	//加载session中存储的数据
	var a = no_cache_url("shopping.action");
	$.get(a, {},
	function(c) {
		if ($("#bottom-cart").size() >= 1) {
			$("#bottom-cart").remove()
		}
		$("body").append(c);
		$(window).scroll();
		if (typeof b != "undefined") {
			b(c)
		}
	})
};
bottom_cart.clear = function(b) {
	//清楚购物车里所有数据
	var a = no_cache_url("shopping.action?action=remove");
	$.get(a, {},
	function(c) {
		if (typeof b != "undefined") {
			b(c)
		}
	})
};
bottom_cart.open = function() {
	$("#bottom-cart").find(".line-detail").show();
	$(".cart-detail-table").hide();
	bottom_cart.opened = true;
	if ($("#col-group-" + bottom_cart.active_group_id).size() <= 0) {
		var a = 0;
		$(".col-group").each(function(b) {
			var c = $(this).attr("rel");
			if (c > a) {
				a = c
			}
		});
		bottom_cart.active_group_id = a
	}
	$("#bottom-cart").find(".col-group").removeClass("closed active").show();
	$("#col-group-" + bottom_cart.active_group_id).addClass("active").show();
	$("#bottom-cart").find(".col-desc").hide();
	$("#bottom-cart").find(".col-add-group").show();
	
	
	if ($("#bottom-cart").find(".cart-detail-table").size() <= 1) {
		$("#cart-detail-table-empty").show();
		return true;
	} else {
		$("#cart-detail-table-" + bottom_cart.active_group_id).show();
		return true;
	}
	
};

bottom_cart.close = function() {
	$("#bottom-cart").find(".line-detail").hide();
	$(".cart-detail-table").hide();
	bottom_cart.opened = false;
	$("#bottom-cart").find(".col-group").hide();
	$("#col-group-" + bottom_cart.active_group_id).removeClass("active").addClass("closed").show();
	$("#bottom-cart").find(".col-desc").hide();
	$("#col-desc-" + bottom_cart.active_group_id).show();
	$("#bottom-cart").find(".col-add-group").hide()
};

bottom_cart.add_effect = function(b) {
	if (typeof b != "undefined") {
		var a = $(window).height() - (b.offset().top - $(window).scrollTop()) - 100;
		if (a < 200) {
			a = 200
		}
		$("#cart-add-effect").css({
			left: b.offset().left,
			bottom: a
		})
	}
	$("#cart-add-effect").show().animate({
		bottom: "10px",
		opacity: 0
	},
	800,
	function() {
		$("#cart-add-effect").css({
			bottom: "200px",
			opacity: 1,
			display: "none"
		})
	})
	
};
//设置盒子滚动事件
bottom_cart.scroll_box = function(){
	var  x_top=$("#site-footer").offset().top
	var lazyheight = parseFloat($(window).height()) + parseFloat($(window).scrollTop());
	
	$("#bottom-cart").css("bottom","0px");
	if(lazyheight>parseInt(x_top)){
		var l=lazyheight-parseInt(x_top);
		$("#bottom-cart").css("bottom",l+"px");
	}
	
	
}
$(function(){
	
	//-----------收藏栏-----------------------

//添加图片加载事件
var a = $('<img class="loading-after-click" src="/images/loading.gif" />').appendTo("body").css({
position: "absolute",
zIndex: "99999",
display: "none"
});

//验证当前最终页是否与购物车存储的最终页一致（当购物车里存在值得时候）
var f = function() {
	
	if (cart.restaurant_id > 0 && cart.restaurant_id != restaurant_id) {
		
		var i = false;
		if (!i) {
			$("#modal-cart-not-the-same-restaurant").modal({
				keyboard: true,
				backdrop: "static"
			});
			i = true
		}
		$("#modal-cart-not-the-same-restaurant").modal("show");
		$("#modal-cart-not-the-same-restaurant .btn.complete").click(function() {
			$("#modal-cart-not-the-same-restaurant").modal("hide")
		});
		$("#modal-cart-not-the-same-restaurant .btn.clear-cart").click(function() {
			bottom_cart.clear(function() {
				bottom_cart.load(function(j) {})
			});
			$("#modal-cart-not-the-same-restaurant").modal("hide")
		});
		return false
	}
	return true;
};

//从服务端加载 购物车
bottom_cart.load(function(i) {
	$(".food-item, .image-food-item").removeClass("selected");
	$("#bottom-cart .food-line").each(function(j) {
		var k = $(this).attr("rel");
		$("#food-" + k).addClass("selected")
	});
	f();
});

// 打开 页面显示购物车
bottom_cart.open();
//设置购物车的滚动信息
$(window).bind("scroll", function(){ 
	bottom_cart.scroll_box();
}); 

//点击购物车显示与隐藏事件
$(".line-brief").live("click",
function() {
	if ($(".line-detail").is(":visible")) {
		bottom_cart.close()
	} else {
		bottom_cart.open()
	}
});

//单击购物车选项卡打开或者关闭购物车
$(".col-group").live("click",
function() {
	if ($(this).hasClass("active")) {
		bottom_cart.close();
		return false
	}
	var i = $(this).attr("rel");
	bottom_cart.active_group_id = i;
	bottom_cart.open();
	return false
});
//增加购物车按钮事件
$(".col-add-group").live("click",
function() {
	var j = 0;
	$(".col-group").each(function(k) {
		var l = parseInt($(this).attr("rel"));
		if (l > j) {
			j = l
		}
	});
	bottom_cart.active_group_id = j + 1;
	$("#cart-detail-table-empty").clone().appendTo("#bottom-cart .line-detail").attr("id", "cart-detail-table-" + bottom_cart.active_group_id);
	var i = bottom_cart.active_group_id + 1;
	$(this).html(i + "号购物车").removeClass("col-add-group").addClass("col-group active").attr("id", "col-group-" + bottom_cart.active_group_id).attr("rel", bottom_cart.active_group_id);
	bottom_cart.open(bottom_cart.active_group_id);
	return false
});

//显示购物车详细信息时右上角关闭按钮
$(".cart-close").live("click",
function() {
	bottom_cart.close();
	return false
});

//检查当前购物车是否为空
var e = function(i, j) {
	bottom_cart.load(function(l) {
		if (bottom_cart.opened) {
			bottom_cart.open()
		} else {
			bottom_cart.close()
		}
		$(".food-item, .image-food-item").removeClass("selected");
		$("#bottom-cart .food-line").each(function(m) {
			var n = $(this).attr("rel");
			$("#food-" + n).addClass("selected")
		});
		var k = cart.total_quantity > 0 ? cart.total_quantity: "空";
		$("#topbar-cart-food-num").html("(" + k + ")");
		if (typeof j != "undefined") {
			j(l)
		}
	})
};

var g = function(j) {
	var i = j.offset().top;
	a.css({
		top: i,
		left: j.offset().left + j.width() + 18
	}).show()
};
// 点击菜单中的按钮，向购物车中添加菜
$(".food-action.add, .food-action.book").live("click",
function() {
	
	if (!f()) {
		return false
	}
	//alert("当点击菜单的时候，需要修服务端存储的菜单。修改程序的地方");
	var resId = $(this).attr("resId");
	var menuName = $(this).attr("menuName");
	var copies = $(this).attr("copies");
	var price = $(this).attr("price");
	var resName = $(this).attr("resName");
	var j = no_cache_url("shopping.action?action=add&resId="+resId+"&menuName="+encodeURI(encodeURI(menuName))+"&price="+price+"&copies="+copies+"&resName="+encodeURI(encodeURI(resName)));
	var i = $(this).attr("entity_id");
	var l = bottom_cart.active_group_id;
	var k = 0;
	var m = $(this);
	g($(this));
	$.get(j, {
		entity_id: i,
		parent_entity_id: k,
		group_id: l
	},
	function(n) {
		e(n,
		function(o) {
			bottom_cart.add_effect(m)
		});
		a.hide()
	});
	
	return false
});

$(".cart-action").live("click",
function() {
	g($(this));
	//alert("修改当前href的地址 进行分数的添加和减少");
	var i = no_cache_url($(this).attr("href"));
	$.get(i, {},
	function(j) {
		e(j,
		function(k) {
			a.hide()
		})
	});
	return false
});

//订单按钮显示是否起送
$(".btn-need-add").live("mouseover",
function() {
	$(".btn-need-add").tooltip("show")
});
$(".btn-need-add").live("mouseleave",
function() {
	$(".btn-need-add").tooltip("hide")
});

//买单按钮 买单
$(".cart-btn").live("click",
function() {
	//alert("测试");
	/*
	if (cart.deliver_amount_message !== "") {
		$("#modal-deliver-amount").modal({
			keyboard: true,
			backdrop: "static"
		});
		$("#modal-deliver-amount .modal-body").html("<h3>" + cart.deliver_amount_message + "</h3");
		$("#modal-deliver-amount").modal("show");
		$("#modal-deliver-amount .btn.complete").live("click",
		function() {
			$("#modal-deliver-amount").modal("hide");
			return false
		});
		return false
	}*/
	//alert(window.location.href);
	$("#topbar_cart").click();
	window.location.href="shopping.action?action=checkOrder&returnURL="+encodeURIComponent(window.location.href);
	//window.location.href="shopping.action?action=checkOrder";
	return false
});
//-----------/收藏栏-----------------------
//左侧边栏 	
var a = $(".food-category-selector");
$(".food-category-selector").show();
$(".trigger").click(function() {
	if ($(this).next().is(":visible")) {
		$(this).next().hide();
		$(this).show();
		a.css({
			marginLeft: 0 - 25
		})
	} else {
		$(this).hide();
		$(this).next().show();
		a.css({
			marginLeft: 0 - a.width()
		})
	}
});
$("body").click(function(b) {
	if (! ($(b.target).parents().is(".food-category-selector"))) {
		$(".food-category-selector .list").hide();
		$(".food-category-selector .trigger").show();
		a.css({
			marginLeft: 0 - 25
		})
	}
});
$(".food-category-selector ul li").click(function() {
	var b = $(this).attr("rel");
	$(window).scrollTo($("#food-category-" + b), 800);
	$(".food-category-selector ul li").removeClass("active");
	$(this).addClass("active");
	return false
})

//==========================
});


 